package com.indusfo.spc.service.impl;

import com.indusfo.spc.exception.GlobalException;
import com.indusfo.spc.exception.ModifyFailedException;
import com.indusfo.spc.mapper.FixedDeteDetailMapper;
import com.indusfo.spc.mapper.FixedDeteMapper;
import com.indusfo.spc.pojo.FixedDete;
import com.indusfo.spc.service.FixedDeteService;
import com.indusfo.spc.untlis.StringUtils;
import com.indusfo.spc.vo.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
  *@Description  固定检测项目
  *@Author louk
  *@Param
  *@return
  *@date 2019/11/22 13:44
 */
@Service
public class FixedDeteServiceImpl implements FixedDeteService {

    private  static  final Logger logger = LoggerFactory.getLogger(BadreplyServiceImpl.class);

    @Resource
    private FixedDeteMapper fixedDeteMapper;

    @Resource
    FixedDeteDetailMapper fixedDeteDetailMapper;
    /**
     * 新增
     * @param fixedDete
     * @return
     */
    @Override
    public JSONObject insertFixedDete(FixedDete fixedDete) {
        try{
            String checkParamResult = checkParam(fixedDete);
            if(!StringUtils.isEmpty(checkParamResult)){
                return JSONObject.build(JSONObject.ERROR,checkParamResult);
            }
            int insert = fixedDeteMapper.insert(fixedDete);
            if(insert==0){
                return JSONObject.build(JSONObject.ERROR,"新增失败",0);
            }
        }catch (GlobalException e) {  //这里只捕获自定义异常
            logger.error(e.getMessage(), e);
            return JSONObject.build(JSONObject.ERROR, e.getMessage());
        }
        return JSONObject.build(JSONObject.SUCCESS,"新增成功",1);
    }

    /**
     * 查询固定检测项目
     * @param fixedDete
     * @return
     */
    @Override
    public JSONObject queryFixedDete(FixedDete fixedDete) {
        List<FixedDete> fixedDeteList=new ArrayList<>();
        try{
            Integer pagesize = fixedDete.getPagesize();
            Integer pageindex = fixedDete.getPageindex();
            if(pagesize != null && pageindex != null) {
                fixedDete.setIncept(pagesize*(pageindex - 1));
            }
            fixedDeteList = fixedDeteMapper.selectAllFixedDete(fixedDete);
            if(!fixedDeteList.isEmpty()){
                int countFixedDete = fixedDeteMapper.countFixedDete(fixedDete);
                return JSONObject.oK("查询成功",fixedDeteList,countFixedDete);
            }
        }catch (GlobalException e){
            logger.error(e.getMessage(), e);
            return JSONObject.build(JSONObject.ERROR, e.getMessage());
        }
        return JSONObject.oK("暂无相关数据",fixedDeteList,null);
    }

    /**
     * 启用停用删除
     * @param fixedDeteIds
     * @param dataState
     * @return
     */
    @Override
    @Transactional
    public JSONObject definesFixedDete(Integer[] fixedDeteIds, Integer dataState) {
        try{
            if(fixedDeteIds==null || fixedDeteIds.length==0){
                return JSONObject.build(JSONObject.ERROR,"请选择固定监测项目 ");
            }
            if(dataState==null || "".equals(dataState)){
                return JSONObject.build(JSONObject.ERROR,"数据状态不能为空");
            }
            if(dataState!=1 && dataState!=2 && dataState!=3){
                return JSONObject.build(JSONObject.ERROR,"数据状态错误");
            }
            fixedDeteDetailMapper.defineDetailByFixedDeteIds(fixedDeteIds,dataState);
            int definesFixedDeteCount = fixedDeteMapper.definesFixedDete(fixedDeteIds, dataState);

            if(definesFixedDeteCount==0){
                switch (dataState){
                    case 1:
                        return JSONObject.build(JSONObject.ERROR,"启用失败");
                    case 2:
                        return JSONObject.build(JSONObject.ERROR,"删除失败");
                    case 3:
                        return JSONObject.build(JSONObject.ERROR,"停用失败");
                        default:
                            break;
                }

            }
        }catch (GlobalException e){
            //设置手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            logger.error(e.getMessage(), e);
            return JSONObject.build(JSONObject.ERROR,e.getMessage());
        }
        switch (dataState){
            case 1:
                return JSONObject.build(JSONObject.SUCCESS,"启用成功");
            case 2:
                return JSONObject.build(JSONObject.SUCCESS,"删除成功");
            case 3:
                return JSONObject.build(JSONObject.SUCCESS,"停用成功");
            default:
                return JSONObject.build(JSONObject.SUCCESS,"成功");
        }
    }

    /**
     * 更新
     * @param fixedDete
     * @return
     */
    @Override
    public JSONObject updateFixedDete(FixedDete fixedDete) {
        try {
            if(fixedDete.getFixedDeteId()==null || "".equals(fixedDete.getFixedDeteId())){
                return JSONObject.build(JSONObject.ERROR,"请选择固定检测项目");
            }
            String checkParamResult = checkParam(fixedDete);
            Integer projectTypeId = fixedDeteMapper.queryProjectType(fixedDete.getFixedDeteId());
            if(projectTypeId.intValue()!=4 && fixedDete.getProjectTypeId().intValue()==4){
                int countFixedDetDetail = fixedDeteDetailMapper.countFixedDetDetail(fixedDete.getFixedDeteId());
                if(countFixedDetDetail>0){
                    return JSONObject.build(JSONObject.ERROR,"所选固定检测项目下包含明细,不能对项目类型进行修改");
                }
            }else if(projectTypeId.intValue()==4 && fixedDete.getProjectTypeId().intValue()!=4){
                int countFixedDetDetail = fixedDeteDetailMapper.countFixedDetDetail(fixedDete.getFixedDeteId());
                if(countFixedDetDetail>0){
                    return JSONObject.build(JSONObject.ERROR,"所选固定检测项目下包含明细,不能对项目类型进行修改");
                }
            }
            if(!StringUtils.isEmpty(checkParamResult)){
                return JSONObject.build(JSONObject.ERROR,checkParamResult);
            }
            int updateCount = fixedDeteMapper.updateByFixedDeteId(fixedDete);
            if(updateCount==0){
                return JSONObject.build(JSONObject.ERROR,"更新失败");
            }
        }catch (GlobalException e){
            return JSONObject.build(JSONObject.ERROR,e.getMessage());
        }
        return JSONObject.build(JSONObject.SUCCESS,"更新成功");
    }

    @Override
    public JSONObject queryJudgeFixedDete(FixedDete fixedDete) {
        List<FixedDete> fixedDeteList=fixedDeteMapper.queryJudgeFixedDete(fixedDete);
        return JSONObject.build(JSONObject.SUCCESS,"查询成功",fixedDeteList);
    }

    /**
     * 校验参数
     * @param fixedDete
     */
    public String checkParam(FixedDete fixedDete){
        //
        String fixedDeteCode = fixedDete.getFixedDeteNo();
        //下限值
        Double lowerLimit = fixedDete.getLowerLimit();
        //中心值
        Double middleLimit = fixedDete.getMiddleLimit();
        //上限值
        Double upperLimit = fixedDete.getUpperLimit();
        //项目类型id
        Integer projectTypeId = fixedDete.getProjectTypeId();
        //单位id
        Integer deteUnitId = fixedDete.getDeteUnitId();

        if(StringUtils.isBlank(fixedDeteCode)){
            return "固定检测项目编码不能为空";
        }
        String deteName = fixedDete.getDeteName();
        if(StringUtils.isEmpty(deteName)){
            return "检测项目名称为空";
        }
        int codeNum = fixedDeteMapper.checkfixedDeteNo(fixedDete);
        if(codeNum>0){
            return "固定检测项目编码已存在";
        }
        if(lowerLimit!=null && !"".equals(lowerLimit) && middleLimit!=null && !"".equals(middleLimit)){
            if(lowerLimit>middleLimit){
                return "下限值不能大于中心值";
            }
        }
        if(lowerLimit!=null && !"".equals(lowerLimit) && upperLimit!=null && !"".equals(upperLimit)){
            if(lowerLimit>=upperLimit){
                return "下限值不能大于上限值";
            }
        }
        if(middleLimit!=null && !"".equals(middleLimit) && upperLimit!=null && !"".equals(upperLimit)){
            if(middleLimit>upperLimit){
               return "中心值不能大于上限值";
            }
        }
        if(projectTypeId==null || "".equals(projectTypeId)){
            return "项目类型不能为空";
        }
        if(deteUnitId==null || "".equals(deteUnitId)){
            return "检测项目单位不能为空";
        }
        return null;

    }
}
